
package edu.uoc.tds.server;


import javax.swing.SwingUtilities;
import javax.swing.JPanel; 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;  
import javax.swing.JOptionPane;
import java.awt.Rectangle; 
import java.awt.Color;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.net.ConnectException;
import edu.uoc.tds.common.UtilLenguaje;



public class Servidor extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel Stat = null;
	private JButton Iniciar = null;
	private JButton Parar = null;
	private UtilLenguaje txt = null;
	private boolean strt = false;
	private RemotoImpl objServeis = null;

   
	private JButton getIniciar() {
 	if (Iniciar == null) {
			Iniciar = new JButton();
			Iniciar.setBounds(new Rectangle(15, 60, 150, 50));
			Iniciar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						iniciarServidor();
					} catch (RemoteException eR) {
                                                eR.printStackTrace();
						JOptionPane.showMessageDialog(null,
								txt.getMessage("servidor.err.rmi"),
								txt.getMessage("servidor.err"), JOptionPane.ERROR_MESSAGE);
					} catch (ConnectException eCe) {
						JOptionPane.showMessageDialog(null,
								txt.getMessage("servidor.err.rmi"),
								txt.getMessage("servidor.err"), JOptionPane.ERROR_MESSAGE);
					} catch (MalformedURLException eMurl) {
						// path incorrecte
						JOptionPane.showMessageDialog(null,
								txt.getMessage("servidor.err.rmi"),
								txt.getMessage("servidor.err"), JOptionPane.ERROR_MESSAGE);
					} catch (Exception eV) {
						// error desconegut
						JOptionPane.showMessageDialog(null,
								eV.toString(),
								txt.getMessage("errors"), JOptionPane.ERROR_MESSAGE);
					}
				}
			});
		}
		return Iniciar;
	}

	
	private JButton getParar() {
		if (Parar == null) {
			Parar = new JButton();
			Parar.setBounds(new Rectangle(223, 60, 150, 50));
			Parar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try	{
						detenerServidor();
					} catch (RemoteException eRe) {
						JOptionPane.showMessageDialog(null,
								txt.getMessage("servidor.err.fi.ok"),
								txt.getMessage("servidor.err"), JOptionPane.ERROR_MESSAGE);
					} catch (MalformedURLException eMurle) {
						// path incorrecte
						JOptionPane.showMessageDialog(null,
								txt.getMessage("servidor.err.fi"),
								txt.getMessage("servidor.err"), JOptionPane.ERROR_MESSAGE);
					} catch (NotBoundException eNbe)	{
						// error
						JOptionPane.showMessageDialog(null,
								txt.getMessage("servidor.err.fi"),
								txt.getMessage("servidor.err"), JOptionPane.ERROR_MESSAGE);
					} catch (Exception eE) {
						// error indeterminat
						JOptionPane.showMessageDialog(null,
								eE.toString(),
								txt.getMessage("errors"), JOptionPane.ERROR_MESSAGE);
					}
				}
			});
		}
		return Parar;
	}

	
	public static void main(String[] args) {
            System.out.println(System.getProperty("user.dir"));
                    SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Servidor thisClass = new Servidor();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	
	public Servidor() {
		super();
		initialize();
	}

	
	private void initialize() {
		
		boolean hiHaFitxerMessages = txt.setDefaultLanguage("i18n/messages");
		if (!hiHaFitxerMessages) {
			System.exit(-1);
		}
		this.setSize(450, 200);
		this.setContentPane(getJContentPane());
		this.setTitle(txt.getMessage("servidor.titol"));
		Iniciar.setText(txt.getMessage("servidor.iniciar"));
		Parar.setText(txt.getMessage("servidor.parar"));
		setStat();
	}

	
	private void initObjecteRemot() throws RemoteException
	{
		strt = false;
		//
		if (objServeis == null) objServeis = new RemotoImpl();
	}

	
	private void setStat () {
		if (!strt){
			Stat.setText(txt.getMessage("servidor.stat.aturat"));
			Iniciar.setEnabled(true);
			Parar.setEnabled(false);
		} else {
			Stat.setText(txt.getMessage("servidor.stat.arrancat"));
			Stat.setForeground(new Color(51, 51, 252));
			Iniciar.setEnabled(false);
			Parar.setEnabled(true);
		}
	}

	
	private void iniciarServidor() throws RemoteException, MalformedURLException, ConnectException
	{
		if (objServeis == null)
			initObjecteRemot();


		Naming.rebind("//localhost/Remoto", objServeis);
		strt = true;
		setStat();
	}

	
	private void detenerServidor() throws RemoteException, MalformedURLException, NotBoundException
	{
		Naming.unbind("//localhost/Remoto");
		strt = false;
		setStat();
	}


	private JPanel getJContentPane() {
		if (jContentPane == null) {
			Stat = new JLabel();
			Stat.setBounds(new Rectangle(17, 8, 347, 16));
			Stat.setForeground(new Color(51, 51, 51));
			Stat.setText("");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(Stat, null);
			jContentPane.add(getIniciar(), null);
			jContentPane.add(getParar(), null);
		}
		return jContentPane;
	}

}
